---
title: "Spring Cloud Bus & Refresh Scope"
date: 2020-03-06
categories:
- java
- spring
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#orgcae6527">EnvironmentChangeEvent</a></li>
<li><a href="#orge947666">Refresh Scope</a></li>
</ul>
</div>
</div>
<blockquote>
<p>
<a href="https://gist.github.com/dsyer/a43fe5f74427b371519af68c5c4904c7">https://gist.github.com/dsyer/a43fe5f74427b371519af68c5c4904c7</a>
</p>
</blockquote>
<div class="outline-2" id="outline-container-orgcae6527">
<h2 id="orgcae6527">EnvironmentChangeEvent</h2>
<div class="outline-text-2" id="text-orgcae6527">
<p>
The application will listen for an EnvironmentChangeEvent and react to the change in a couple of standard ways (additional ApplicationListeners can be added as @Beans by the user in the normal way). When an EnvironmentChangeEvent is observed it will have a list of key values that have changed, and the application will use those to:
</p>
<ul class="org-ul">
<li>Re-bind any @ConfigurationProperties beans in the context</li>
<li>Set the logger levels for any properties in logging.level.*</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-orge947666">
<h2 id="orge947666">Refresh Scope</h2>
<div class="outline-text-2" id="text-orge947666">
<p>
Note that all beans in this scope are only initialized when first accessed, so the scope forces lazy initialization semantics. The implementation involves creating a proxy for every bean in the scope, so there is a flag
</p>
<p>
If a bean is refreshed then the next time the bean is accessed (i.e. a method is executed) a new instance is created. All lifecycle methods are applied to the bean instances, so any destruction callbacks that were registered in the bean factory are called when it is refreshed, and then the initialization callbacks are invoked as normal when the new instance is created. A new bean instance is created from the original bean definition, so any externalized content (property placeholders or expressions in string literals) is re-evaluated when it is created.
</p>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2020-03-06</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
